package com.aoxon.cargo.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.Note;
import com.aoxon.cargo.component.MyCallBack;
import com.aoxon.cargo.component.MyTextWatcher;
import com.aoxon.cargo.shared.NoteShared;
import com.aoxon.cargo.util.CurrentTimeUtil;
import com.aoxon.cargo.util.DataUtil;
import com.aoxon.cargo.util.ToastUtil;

/**
 * 采购端便签记录的填写页面
 * 
 * @author ZZJ
 * 
 */
public class PurNoteInputActivity extends BaseActivity {

	public static boolean isNewAdd = true;

	private EditText etTitle;
	private EditText etContent;

	private ImageView ivReturnButton;
	private ImageView ivCommitButton;

	private TextView tvTitle;

	private MyTextWatcher myTextWatcher;

	private List<Note> notes;
	private Note note = new Note();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setup();

	}

	private void setup() {
		setContentView(R.layout.pur_note_input);

		etTitle = (EditText) findViewById(R.id.etPurNoteInputTitle);
		etContent = (EditText) findViewById(R.id.etPurNoteInputContent);

		ivReturnButton = (ImageView) findViewById(R.id.ivPublicTitleReturn);
		ivCommitButton = (ImageView) findViewById(R.id.ivPublicTitleOther);

		tvTitle = (TextView) findViewById(R.id.tvPublicTiTle);

		myTextWatcher = new MyTextWatcher(myCallBack);

		ivCommitButton.setEnabled(false);
		ivCommitButton.setImageResource(R.drawable.button_public_commit);

		if (isNewAdd) {
			tvTitle.setText("新便签");
		} else {
			tvTitle.setText("便签修改");

			Intent intent = getIntent();
			Bundle bundle = intent.getExtras();
			String strNote = bundle.getString("strNote");

			if (strNote != null && !strNote.equals("")) {
				note = DataUtil.gson.fromJson(strNote, Note.class);

				etTitle.setText(note.getTitle());
				etContent.setText(note.getContent());

				notes = NoteShared.getNoteList();
			}
		}

		etTitle.addTextChangedListener(myTextWatcher);
		etContent.addTextChangedListener(myTextWatcher);

		ivReturnButton.setOnClickListener(myClickListener);
		ivCommitButton.setOnClickListener(myClickListener);
	}

	MyCallBack myCallBack = new MyCallBack() {

		@Override
		public void callback(String choose) {
			// TODO Auto-generated method stub

			String strTitle = etTitle.getText().toString();
			String strContent = etContent.getText().toString();
			if (strTitle != null && strContent != null && !strTitle.equals("")
					&& !strContent.equals("")) {
				ivCommitButton.setEnabled(true);
			}else{
				ivCommitButton.setEnabled(false);
			}
		}
	};

	
	OnClickListener myClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			
			switch (v.getId()) {
			
			case R.id.ivPublicTitleReturn:
				
				break;
			
			case R.id.ivPublicTitleOther:
				String time = CurrentTimeUtil.getCurrentTimeString();
				
				note.setTitle(etTitle.getText().toString());
				note.setContent(etContent.getText().toString());
				note.setTime(time);
				
				if(isNewAdd){
					note.setId(time);
					NoteShared.saveNote(note);
				}else{
					boolean isRevamp = false;
					for(int i = 0; i< notes.size(); i ++){
						if(note.getId().equals(notes.get(i).getId())){
							notes.remove(i);
							isRevamp = true;
							break;
						}
					}
					note.setId(time);
					if(isRevamp){
						notes.add(note);
						NoteShared.saveNoteList(notes);
					}else{
						return ;
					}
					
				}
				ToastUtil.show(getBaseContext(), "资料保存成功");
				break;

			default:
				break;
			}
			Intent intent = new Intent(getBaseContext(), PurNoteListActivity.class);
			startActivity(intent);
			PurNoteInputActivity.this.finish();
		}
	};
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent intent = new Intent(getBaseContext(),
					PurNoteListActivity.class);
			startActivity(intent);
			this.finish();
			return false;
		}
		return false;
	}

}
